package com.animee.day11.slice;

import com.animee.day11.ResourceTable;
import com.animee.day11.bean.FilmTypeBean;
import com.animee.day11.utils.FilmTypeProvider;
import com.animee.day11.utils.RawFileUtils;
import com.google.gson.Gson;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;

import java.util.ArrayList;
import java.util.List;
/**
 * 1绘制当前布局，包含ListContainer
 * 2.绘制每一个item的布局，
 *3.再java代码中找到listContainer
 * */
public class FilmTypeAbilitySlice extends AbilitySlice {
    ListContainer lc;
    List<FilmTypeBean.FilmTypesDTO>mDatas = new ArrayList<>();
    FilmTypeProvider provider;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_abilitiy_filmtype);
        initView();
        getData();
    }
    private void getData() {
        String json = RawFileUtils.readRawFile(this, "film.json");
        FilmTypeBean typeBean = new Gson().fromJson(json, FilmTypeBean.class);
        List<FilmTypeBean.FilmTypesDTO> list = typeBean.getFilmTypes();
        //将加载到的数据，放入数据源
        mDatas.addAll(list);
        //提示适配器更新
        provider.notifyDataChanged();
    }
    private void initView() {
        lc = findComponentById(ResourceTable.Id_ftype_lc);
        //生成适配器对象
        provider = new FilmTypeProvider(this, mDatas);
        //绑定适配器
        lc.setItemProvider(provider);
        //给listcontainer的每一项添加点击事件
        lc.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
                FilmTypeBean.FilmTypesDTO dto = mDatas.get(i);
                Intent intent = new Intent();
                intent.setParam("film",dto); //如果对象实现了序列化接口，可以直接传递对象
                present(new FilmListAbilitySlice(),intent);
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
